package com.cloudptt.wp.ui.more.advanced;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.cloudptt.api.comm.Const;
import com.cloudptt.wp.R;

public class CustomPrefListActivity extends Activity {
	
	private SharedPreferences sharedPreferences;
	KeyListAdapter keyAdapter;
	ListView keyListView;
	TextView backView;
	int lastValue,curLan;
	
	int key;
	int keyResourceID;
	int arrayListResourceID;
	
	List<String> keyItemList = null;
	private Map<Integer, Boolean> isSelected; 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView( R.layout.language_selection );
		
		sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		
		Intent intent = getIntent();
		key = intent.getIntExtra(Const.INTENT_KEY,0);
		
		TextView titleTextV = (TextView)findViewById(R.id.topbar_title);
		titleTextV.setText( getString(key) );
		
		keyResourceID = intent.getIntExtra( Const.INTENT_KEY_RESOURCE_ID, 0);
		arrayListResourceID = intent.getIntExtra( Const.INTENT_ARRAY_RESOURCEID, 0);
		
		String[] keyArray = getResources().getStringArray( arrayListResourceID );
		keyItemList = Arrays.asList( keyArray );
		
		if (isSelected != null)  
            isSelected = null;  
        isSelected = new HashMap<Integer, Boolean>();  
        for (int i = 0; i < keyItemList.size(); i++) {  
            isSelected.put(i, false);  
        }
        lastValue = sharedPreferences.getInt( getString( keyResourceID), 0);
		
		isSelected.put(lastValue, true);
		
		backView = (TextView)findViewById(R.id.back);
		backView.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() 
        	    {   
        	        @Override
        	        public void onClick(DialogInterface dialog, int which) 
        	        {
        	        	CustomPrefListActivity.this.finish();
        	        }
        	     };
        	    finish();
				return false;
			}
		});
		
		
		keyListView = (ListView)findViewById(R.id.language_list);
		keyAdapter = new KeyListAdapter(this);
		keyListView.setAdapter( keyAdapter  );
		keyListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		keyAdapter.notifyDataSetChanged();
		keyListView.setOnItemClickListener( new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View v, int pos,
					long id) {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putInt( getString(keyResourceID), pos);
                editor.commit();
                for(Integer p : isSelected.keySet()) {  
                    isSelected.put(p, false);  
                }  
                isSelected.put(pos, true);
                keyAdapter.notifyDataSetChanged();
			}
		});
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	
	public class KeyListAdapter extends BaseAdapter{

		private LayoutInflater mInflater;
        
        
        public KeyListAdapter(Context context){
            this.mInflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return keyItemList.size();
        }
 
        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return keyItemList.get(position);
        }
 
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
 
        public final class ViewHolder{
            public TextView title;
            public CheckBox checkBox;
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            final int pos = position;
            if (convertView == null) {
                holder=new ViewHolder();  
                convertView = mInflater.inflate(R.layout.language_item, null);
                holder.title = (TextView)convertView.findViewById(R.id.language_item);
                holder.checkBox = (CheckBox)convertView.findViewById(R.id.item_checkbox_state);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder)convertView.getTag();
            }

            holder.title.setText((String)keyItemList.get(position));
            holder.checkBox.setChecked(isSelected.get(position));
             
            return convertView;
        }
	}
	
}
